<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '300px'
        },
        dates: {
            type: Array,
            default: []
        }
    },
    watch: {
        dates: {
            handler(val) {
                console.log(val);

                this.setChartOptions(val);
            },
            deep: true
        }
    },
    data() {
        return {
            chart: null
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setChartOptions(this.dates);
        },
        setChartOptions(timerange) {
            const data = [
                { city: '石家庄市', time: 3 },
                { city: '张家口市', time: 6 },
                { city: '秦皇岛市', time: 4 },
                { city: '邢台市', time: 5 },
                { city: '邯郸市', time: 8 },
            ]
            const maxArray = new Array(data.map(item => item.time).length).fill(Math.max(...(data.map(item => item.time))));
            const points = data.map(item => {
                let obj = {
                    name: 'top-line',
                    xAxis: item.city,
                    yAxis: item.time,
                    symbol: 'rect',
                    symbolSize: [17, 3],
                    itemStyle: {
                        color: 'rgba(255, 255, 255, 1)',
                        shadowColor: 'rgba(255, 255, 255, 1)',
                        shadowBlur: 10,
                        shadowOffsetY: -3
                    }
                }
                return obj
            })

            let option = {
                tooltip: {
                    show: true,
                    trigger: "item",
                    // backgroundColor: 'rgba(21, 154, 255, 0.32)',
                    textStyle: {
                        color: '#fff'
                    },
                },
                grid: {
                    top: "10%",
                    left: "3%",
                    right: "3%",
                    bottom: "0%",
                    containLabel: true
                },
                // 添加第二个 xAxis 用于折线图偏移
                xAxis: {
                    type: "category",
                    data: timerange,
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: "#242424",
                        fontSize: 12,
                        margin: 20
                    },
                    axisLine: {
                        lineStyle: {
                            color: "rgba(19, 44, 95, 1)",
                            width: 1
                        }
                    },
                },
                yAxis: {
                    type: 'value',
                    name: '度',
                    // nameGap: 35,
                    nameTextStyle: {
                        color: '#242424',
                        fontSize: 12
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#3C6579'
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    splitLine: {
                        lineStyle: {
                            // color: '#173055',
                            // type: 'dashed'
                        }
                    },
                    axisLabel: {
                        color: '#242424',
                        fontSize: 12
                    },
                },
                series: [
                    {
                        type: "bar",
                        barWidth: 16,
                        name: "度数",
                        data: data.map(item => item.time),
                        itemStyle: {
                            borderRadius: [0, 0, 0, 0],
                            color: {
                                type: 'linear',
                                x: 0, y: 0, x2: 0, y2: 1,
                                colorStops: [
                                    { offset: 0, color: 'rgba(86, 176, 254, 1)' },
                                    { offset: 0.1, color: 'rgba(68, 163, 255, 1)' },
                                    { offset: 1, color: 'rgba(46, 140, 255, 1)' }
                                ],
                                global: false
                            }
                        },
                        markPoint: {
                            symbol: 'none',
                            silent: true,
                            data: points
                        },
                    },
                ],
                dataZoom: [{
                    type: 'slider',
                    show: false,
                    // startValue: data.length - 14, // 展示后14个数据的索引
                    // endValue: data.length // 展示全部数据
                }, {
                    type: 'inside', // 这个 dataZoom 组件是 inside 型 dataZoom 组件
                    start: 0,      // 左边在 0% 的位置。
                    end: 10       // 右边在 10% 的位置。
                }]
            };

            this.chart.setOption(option)
        }
    }
}
</script>
